package my.sample.snmp;

import java.util.ArrayList;
import java.util.List;
import org.snmp4j.PDU;
import org.snmp4j.ScopedPDU;
import org.snmp4j.Snmp;
import org.snmp4j.TransportMapping;
import org.snmp4j.UserTarget;
import org.snmp4j.event.ResponseEvent;
import org.snmp4j.mp.MPv3;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.security.AuthMD5;
import org.snmp4j.security.PrivDES;
import org.snmp4j.security.SecurityLevel;
import org.snmp4j.security.SecurityModels;
import org.snmp4j.security.SecurityProtocols;
import org.snmp4j.security.USM;
import org.snmp4j.security.UsmUser;
import org.snmp4j.smi.Address;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.UdpAddress;
import org.snmp4j.smi.VariableBinding;
import org.snmp4j.transport.DefaultUdpTransportMapping;

public class snmpgetnextv3 {

	public static List<String> snmpGet()
	{
	 List<String> list = new ArrayList<String>();
	 	int snmpVersion=3;
	 	String strAddress =null;
		try
		{	
			TransportMapping transport = new DefaultUdpTransportMapping();
	        USM usm = new USM(SecurityProtocols.getInstance(), new OctetString(MPv3.createLocalEngineID()), 0);
            SecurityModels.getInstance().addSecurityModel(usm);
            Snmp snmp = new Snmp(transport);
            snmp.getUSM().addUser(
                    new OctetString("AuthPrivTest"),
                    new UsmUser(new OctetString("AuthPrivTest"), AuthMD5.ID, new OctetString("AuthPrivTest"), PrivDES.ID,new OctetString("AuthPrivPass")));
	        transport.listen();
			strAddress= "119.226.225.244"+"/" + 161;
			Address targetaddress = new UdpAddress(strAddress);
			UserTarget target = new UserTarget();
			if(snmpVersion == 1){
				target.setVersion(SnmpConstants.version1);
			}
			if(snmpVersion == 2){
				target.setVersion(SnmpConstants.version2c);
			}
			if(snmpVersion == 3){
				target.setVersion(SnmpConstants.version3);
			}
			target.setAddress(targetaddress);
			target.setRetries(2);
			target.setTimeout(5000);
			target.setSecurityLevel(SecurityLevel.AUTH_PRIV);
			target.setSecurityName(new OctetString("AuthPrivTest"));
			
			ResponseEvent response;
			PDU pdu = new ScopedPDU();
			pdu.setType(PDU.GET);
			pdu.add(new VariableBinding(new OID(".1.3.6.1.2.1.1.1.0")));
	          
			
			response = snmp.get(pdu,target);
			
			if(response != null)
			{
		   PDU pduresponse=response.getResponse();										
           System.out.println(pduresponse.getVariableBindings());
           list.add(pduresponse.getVariableBindings().toString());
           
			}
			else
			{
				System.out.println("Feeling like a TimeOut occured ");
			}
			snmp.close();
		} catch(Exception e) { e.printStackTrace(); }

		return list;
			
		}}